// https://idol.st/allstars/card/431

let nameListDom = document.createElement("div");
nameListDom.style.display = "none";

let cardUrlListDom = document.createElement("div");
cardUrlListDom.style.display = "none";

let bodyDom = document.getElementsByTagName("body")[0];
bodyDom.appendChild(nameListDom);
bodyDom.appendChild(cardUrlListDom);

let nameHTML = "";
let cardUrlHTML = "";

let nameTeamMap = {
    高坂穂乃果:  { name: "高坂穗乃果", team:  "μ's"},
    南ことり:   { name:  "南小鸟", team:  "μ's"},
    園田海未:   { name:  "园田海未", team:  "μ's"},
    絢瀬絵里:   { name:  "绚濑绘里", team:  "μ's"},
    東條希:    { name:   "东条希", team:  "μ's"},
    矢澤にこ:   { name:  "矢泽妮可", team:  "μ's"},
    星空凛:    { name:   "星空凛", team:  "μ's"},
    西木野真姫:  { name: "西木野真姬", team:  "μ's"},
    小泉花陽:   { name:  "小泉花阳", team:  "μ's"},

    高海千歌:   { name:  "高海千歌", team: "Aqours"},
    桜内梨子:   { name:  "樱内梨子", team: "Aqours"},
    渡辺曜:    { name:   "渡边曜", team: "Aqours"},
    津島善子:   { name:  "津岛善子", team: "Aqours"},
    国木田花丸:  { name: "国木田花丸", team: "Aqours"},
    黒澤ルビィ:  { name: "黑泽露比", team: "Aqours"},
    小原鞠莉:   { name:  "小原鞠莉", team: "Aqours"},
    松浦果南:   { name:  "松浦果南", team: "Aqours"},
    黒澤ダイヤ:  { name: "黑泽黛雅", team: "Aqours"},

    優木せつ菜: { name: "优木雪菜", team: "虹团"},
    中須かすみ: { name: "中须霞", team: "虹团"},
    桜坂しずく: { name: "樱坂雫", team: "虹团"},
    天王寺璃奈: { name: "天王寺璃奈", team: "虹团"},
    朝香果林: { name: "朝香果林", team: "虹团"},
    近江彼方: { name: "近江彼方", team: "虹团"},
    "エマ・ヴェルデ": { name: "艾玛·维尔德", team: "虹团"},
    上原歩夢: { name: "上原步梦", team: "虹团"},
    宮下愛: { name: "宫下爱", team: "虹团"},
    三船栞子: { name: "三船栞子", team: "虹团"}
};


for (let i = 289; i <= 482; i++) {
    core(i)
}


nameListDom.innerHTML = nameHTML;
cardUrlListDom.innerHTML = cardUrlHTML;
console.log("end")

function core(i) {
    let xml = new XMLHttpRequest();
    xml.open("GET", "https://idol.st/allstars/card/" + i, false);

    xml.onreadystatechange = function() {
        if (xml.readyState === 4 && xml.status === 200) {

            let cardId = "000" + i;
            cardId = cardId.substring(cardId.length - 4);

            let bodyDom = document.createElement("div");
            bodyDom.innerHTML = xml.responseText;
            let infoDom = bodyDom.querySelector(".table.about-table tbody");

            let idolName = infoDom.querySelector("tr[data-field='idol'] td span.text_with_link").innerText;
            idolName = idolName.substring(0, idolName.indexOf('\n'));
            idolName = idolName.replaceAll(" ", "");
            let team = "NULL";
            if (nameTeamMap[idolName] !== undefined) {
                team = nameTeamMap[idolName]["team"];
                idolName = nameTeamMap[idolName]["name"];
            }

            let skill = infoDom.querySelector("tr[data-field='name'] td span").innerText.replaceAll("-", "_");


            let skillIdolized = infoDom.querySelector("tr[data-field='name_idolized'] td span").innerText.replaceAll("-", "_");

            let rarity = infoDom.querySelector("tr[data-field='rarity'] > td:nth-child(3)").innerText.trim();
            switch (rarity) {
                case "Ultra rare":
                    rarity = "UR";
                    break;
                case "Sレア":
                    rarity = "SR";
                    break;
                case "レア":
                    rarity = "R";
                    break;
            }

            // dealCard(infoDom.querySelector("tr[data-field='art'] > td:nth-child(3)"), team, cardId, idolName, "普通", rarity, skill);
            // dealCard(infoDom.querySelector("tr[data-field='art_idolized'] > td:nth-child(3)"), team, cardId, idolName, "觉醒", rarity, skillIdolized);

            dealTransCard(infoDom.querySelector("tr[data-field='transparent']"), team, cardId, idolName, "普通", rarity, skill);
            dealTransCard(infoDom.querySelector("tr[data-field='transparent_idolized']"), team, cardId, idolName, "觉醒", rarity, skillIdolized);

        }
    }
    xml.send();
}

function dealCard(cardDom, team, cardId, idolName, idolized, rarity, skill) {
    let cardClickDom = cardDom.querySelector("div.image-with-links");
    let url = "";
    if (cardClickDom === null) {
        url = cardDom.querySelector("a").href;
    } else {
        cardClickDom.click();
        url = cardDom.querySelector(".list-group-item a").href;
    }
    let id = getId(url);
    let name = id + "~~~SIFAS壁纸-" + cardId + "-" + team + "-" + idolName + "-" + idolized + "-" + rarity + "-" + skill;
    nameHTML += name + "<br />";
    cardUrlHTML += url + "<br />";
    console.log(name);
}

function dealTransCard(cardDom, team, cardId, idolName, idolized, rarity, skill) {
    if (cardDom === null) {
        return ;
    }
    let url = cardDom.querySelector("td:nth-child(3) > a").href;
    let id = getId(url);
    let name = id + "~~~SIFAS立绘-" + cardId + "-" + team + "-" + idolName + "-" + idolized + "-" + rarity + "-" + skill;
    nameHTML += name + "<br />";
    cardUrlHTML += url + "<br />";
    console.log(name);
}

function getId(url) {
    return url.substring(url.lastIndexOf('/') + 1, url.lastIndexOf('.'));
}

